<?php
/*
Plugin Name: Main Navigation
Plugin URI: 
Version: v1.0
Author: <a href="http://huynhuan.wordpress.com">HuanHH</a>
Description: Power by Cao Dang Nghe Long An.
*/
?>
<?php 
class MainNav extends WP_Widget {

	function MainNav() {
		$widget_ops = array('classname' => 'main_nav', 'description' => 'Main Navication' );
		$control_ops = array ('width' => '200', 'height' => '400');
		$this->WP_Widget('main_nav', 'Main Navication', $widget_ops, $control_ops);
	}
	
	function widget($args, $instance) {
		extract($args);
		extract($instance);
		$menus = get_categories(array('parent'=>$menu, 'hide_empty'=>false));
		//pr($menus);
		global $post;
		$current_cat = get_query_var('cat', 0);
		$current_cat = ($current_cat==0 && !is_single()) ? $home_page : $current_cat;
		if ($current_cat == 0) {
			$category_ids = wp_get_post_categories($post->ID);
			//pr($category_ids);
			$current_cat = isset($category_ids[0]) ? $category_ids[0] : 0;
		}
		$current_category = get_category($current_cat);
		//pr($category);
		?>
		
		<div id="box">
			<div class="top">
				<div class="title"><?php print $title; ?></div>
			</div>
			<div class="center">
				<?php if (count($menus) > 0) : ?>
				<ul>
					<?php foreach ($menus as $item) : ?>
						<li><a <?php print ($current_cat==$item->term_id || $current_category->parent==$item->term_id) ? 'class="current"' : ''?> href="<?php print ($item->term_id==$home_page) ? home_url() : get_category_link($item->term_id)?>"><?php print $item->name?></a>
						<?php if (($current_cat != 0 && $current_cat == $item->term_id) || $current_category->parent==$item->term_id) : 
							$submenus = get_categories(array('parent'=>$item->term_id, 'hide_empty'=>false));
							if (count($submenus) > 0) : ?>
								<ul class="sub">
									<?php foreach ($submenus as $subitem) : ?>
										<li><a <?php print ($current_cat==$subitem->term_id) ? 'class="current"' : ''?> href="<?php print get_category_link($subitem->term_id)?>"><?php print $subitem->name?></a></li>
									<?php endforeach; ?>
								</ul>
							<?php endif; ?>
						<?php endif; ?>
						</li>
					<?php endforeach; ?>
				</ul>
				<?php endif; ?>
			</div>
			<div class="bot"></div>
		</div>
		<?php
	}

	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$title = $new_instance['title'];
		$menu = $new_instance['menu'];
		$home_page = $new_instance['home_page'];
		$instance = compact('title', 'menu', 'home_page');
		return $instance;
	}

	function form($instance) {
		$defaults=array(
			'title' => __('Danh mục', 'cdnla'),
			'menu' => 0,
			'home_page' => 0
		);
		
		$options = wp_parse_args($instance, $defaults);
		extract($options);
		$parent_cat = get_categories(array('parent'=>0, 'hide_empty'=>false));
		
		$child_cat = array();
		if ($menu != 0) {
			$child_cat = get_categories(array('parent'=>$menu, 'hide_empty'=>false));
		}
		?>

		<div>
			<div>Title:</div>
			<div><input  id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></div>
			<div>Chọn danh mục</div>
			<div>
				 <select  id="<?php echo $this->get_field_id('menu'); ?>" name="<?php echo $this->get_field_name('menu'); ?>">
					<option value="">--None--</option>
					<?php if ($parent_cat != NULL) : ?>
						<?php foreach ($parent_cat as $k => $v) : ?>
							<option value="<?php print $v->term_id; ?>" <?php print ($menu==$v->term_id) ? "selected='selected'" : ""; ?>><?php print $v->name; ?></option>
						<?php endforeach; ?>
					<?php endif; ?>
				</select>
			</div>
			<?php if (NULL != $child_cat) : ?>
				<div>Trang chủ</div>
				<div>
					<select  id="<?php echo $this->get_field_id('home_page'); ?>" name="<?php echo $this->get_field_name('home_page'); ?>">
						<option value="">--None--</option>
						<?php foreach ($child_cat as $k => $v) : ?>
							<option value="<?php print $v->term_id; ?>" <?php print ($home_page==$v->term_id) ? "selected='selected'" : ""; ?>><?php print $v->name; ?></option>
						<?php endforeach; ?>
					</select>
				</div>
			<?php endif; ?>
		</div>

		<?php
	}
}

if (class_exists("MainNav")) {
	$main_nav = new MainNav();
}

function register_main_nav() {
	register_widget('MainNav');
}
add_action('widgets_init', 'register_main_nav');

?>
